package com.kkd.kkdapp.activity;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.utils.DataCleanManager;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.FileUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

public class SetUpActivity extends BaseActivity implements OnClickListener {

	private RelativeLayout top_img; // 后退
	private RelativeLayout about; // 关于
	private RelativeLayout clearcache_rl; // 清理缓存
	private Dialog toLoadDialog; // 清理缓存等待Dialog

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_up);
		initView();
		setListener();
	}

	/***
	 * 初始化控件
	 **/
	public void initView() {
		top_img = (RelativeLayout) this.findViewById(R.id.top_img);
		about = (RelativeLayout) this.findViewById(R.id.about);
		clearcache_rl = (RelativeLayout) this.findViewById(R.id.clearcache_rl);
	}

	/**
	 * 注册监听事件
	 **/
	public void setListener() {
		top_img.setOnClickListener(this);
		about.setOnClickListener(this);
		clearcache_rl.setOnClickListener(this);

	}

	private Intent intent;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_img: // 后退
			finish();
			break;
		case R.id.about: // 关于
			intent = new Intent(this, AboutActivity.class);
			startActivity(intent);
			break;
		case R.id.clearcache_rl: // 清理缓存
			clearCache();
			break;
		default:
			break;
		}
	}

	// 清理本地缓存
	public void clearCache() {
		showToLoadDialog("清理缓存中...");
		PreferenceUtils.getInstance().setFindData(this, null);
		DataCleanManager.cleanExternalCache(this);
		DataCleanManager.cleanCustomCache(FileUtils.getKKDRootPath());
		DataCleanManager.cleanCustomCache(FileUtils.getKKDRootPath() + "pdf");
		DataCleanManager.cleanCustomCache(FileUtils.getKKDRootPath() + "apk");
		DataCleanManager.cleanCustomCache(FileUtils.getKKDRootPath() + "temp");
		DataCleanManager.cleanCustomCache(FileUtils.getKKDRootPath()
				+ "head_icon");
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				closeToLoadDialog();
				showToast("清理完毕");
			}
		}, 1000);
	}

	// 打开清理缓存Dialog
	public void showToLoadDialog(String content) {
		if (toLoadDialog != null) {
			toLoadDialog.cancel();
			toLoadDialog = null;
		}
		toLoadDialog = DialogUtils.toLoadDialog(this, content);
		toLoadDialog.show();
	}

	// 关闭清理缓存Dialog
	public void closeToLoadDialog() {
		if (toLoadDialog != null) {
			toLoadDialog.dismiss();
			toLoadDialog.cancel();
			toLoadDialog = null;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		closeToLoadDialog();
	}
}
